package com.xkdx.guangguang.fragment.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.card.CardDetailFragment;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.newsinfo.DiscountFragment;
import com.xkdx.guangguang.fragment.newsinfo.NewsFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.DiscountInfo;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class DayRecommendFragment extends BaseFragment {
	private LayoutInflater inflater;
	private ListView listView;
	private MyAdapter adapter;
	private List<DiscountInfo> list;
	private List<DiscountInfo> infos;
	private String shopID;
	private int infoType = -1;
	private HomePresenter presenter;

	private String RegionID;
	private int PageIndex = 1;
	private ProgressDialog pd;
	private SharePrefenceUtil su;
	private RelativeLayout frame;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private ImageView imageview;
	private View view;
	private Button btn_nearback;
	private RecommendAction recommendAction;
	private RecommendModule recommendModule;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		inflater = LayoutInflater.from(getActivity());
		list = new ArrayList<DiscountInfo>();
		infos = new ArrayList<DiscountInfo>();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.shop_empty)
				.showImageForEmptyUri(R.drawable.shop_empty)
				.showImageOnFail(R.drawable.shop_empty)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.shop_near_new_product, container,
				false);

		findView();
		setClick();
		showLoading();
		return view;
	}
	
	@Override
	public void findView() {
		listView = (ListView) view.findViewById(R.id.new_product_listview);
		btn_nearback = (Button) view.findViewById(R.id.back_neardiscount); // 回退按钮
		imageview = (ImageView) view.findViewById(R.id.home_default_image);
		
		SharePrefenceUtil su_city = new SharePrefenceUtil(getActivity(), IConstants.SP_CITY);
		recommendAction = new RecommendAction(su_city.getCityID());
		recommendModule = new RecommendModule();
	}

	private void setClick() {
		btn_nearback.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getActivity().finish();
			}
		});
	}

	private void showLoaded() {
//		list = ((HomeNearLocationActivity) getActivity()).getHomeModule().nearInfoList;
		list = recommendModule.nearInfoList;
		adapter = new MyAdapter(list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String infoType = list.get(position).getInfoType();
				FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
				if (infoType.equals("1")) {
					DiscountFragment discountFragment = new DiscountFragment();
					ft.replace(R.id.fragment, discountFragment);
					Bundle b = new Bundle();
					b.putString("infoID", list.get(position).getInfoID());
					discountFragment.setArguments(b);
					ft.addToBackStack(null);
					ft.commit();
				} else if (infoType.equals("5")) {
					CardDetailFragment cardDetailFragment = new CardDetailFragment();
					Bundle b = new Bundle();
					b.putString("CinfoID", list.get(position).getInfoID());
					cardDetailFragment.setArguments(b);
					getFragmentManager().beginTransaction()
							.replace(R.id.fragment, cardDetailFragment)
							.addToBackStack(null).commit();

				} else {
					NewsFragment newsFragment = new NewsFragment();
					ft.replace(R.id.fragment, newsFragment);
					Bundle b = new Bundle();
					b.putString("infoID", list.get(position).getInfoID());
					newsFragment.setArguments(b);
					ft.addToBackStack(null);
					ft.commit();
				}

			}
		});

	}

	class MyAdapter extends BaseAdapter {

		private List<DiscountInfo> list;

		public MyAdapter() {
			super();
		}

		public MyAdapter(List<DiscountInfo> listinfo) {
			super();
			this.list = listinfo;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHodler viewHoder;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.home_nearlocation_item,
						null);
				viewHoder = new ViewHodler();
				viewHoder.image = (ImageView) convertView
						.findViewById(R.id.shop_new_product_image);
				viewHoder.name = (TextView) convertView
						.findViewById(R.id.shop_new_product_tv);
				viewHoder.time = (TextView) convertView.findViewById(R.id.time);
				viewHoder.home_near_shopname = (TextView) convertView
						.findViewById(R.id.home_near_shopname);
				viewHoder.home_near_distance = (TextView) convertView
						.findViewById(R.id.home_near_distance);
				convertView.setTag(viewHoder);
			} else {
				viewHoder = (ViewHodler) convertView.getTag();
			}

			String url = IConstants.addSuffix(list.get(position)
					.getPictrueUrl(), "_cover1");
			viewHoder.name.setText(list.get(position).getInfoTitle());
			imageLoader.displayImage(url, viewHoder.image, options);
			viewHoder.time.setText("活动时间: "
					+ list.get(position).getBeginTime().replace("00:00:00", "")
							.trim()
					+ " 至 "
					+ list.get(position).getEndTime().replace("00:00:00", "")
							.trim());
			viewHoder.home_near_shopname.setText(list.get(position)
					.getShopName());
			viewHoder.home_near_distance.setText(list.get(position)
					.getDistance() + "m");
			return convertView;
		}
	}

	class ViewHodler {
		TextView name, time, home_near_shopname, home_near_distance;
		ImageView image;

	}

	@Override
	public void setOnClick() {
		
	}

	@Override
	public void showLoading() {

		presenter = new HomePresenter(this);
		((ManagerActivity) getActivity()).addCurrentTask(presenter);
		dataLoadDialog.show();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				presenter.cancel(true);
			}
		});
		presenter.setActitons(recommendAction);
		presenter.setModule(recommendModule);
		presenter.execute();
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showClick(HashMap<String, AbsModule> msg) {
		dataLoadDialog.dismiss();
		if (msg.containsKey("0")) {
			recommendModule = (RecommendModule) msg.get("0");
			showLoaded();
		} else {
			showErrorTip(msg);
		}
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub
		
	}

}
